package stocks

import (
	"gitee.com/chenrh/finego/app"
	"gitee.com/chenrh/finego/backends/utils"

	"gitee.com/chenrh/finego/backends/models"
	"gitee.com/chenrh/gotoolkit/conv"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"

	// "strings"

	"time"
)

// CalendarController serves the "/", "/ping" and "/hello".
type CalendarController struct {
	// Optionally: context is auto-binded by Iris on each request,
	// remember that on each incoming request iris creates a new UserController each time,
	// so all fields are request-scoped by-default, only dependency injection is able to set
	// custom fields like the Service which is the same for all requests (static binding).
	Ctx iris.Context

	// Our UserService, it's an interface which
	// is binded from the main application.
	//Service services.UserService
}

// BeforeActivation 注册自定义的路由
func (c *CalendarController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/calendar_events/save", "SaveCalendarEventForm")
	b.Handle("POST", "/calendar_events/getdata", "GetCalendarEventList")
	b.Handle("POST", "/calendar/crawler_notice", "CarwlerNotice")
}

// CarwlerNotice
func (c *CalendarController) CarwlerNotice() {
	kind := c.Ctx.FormValue("kind")
	if kind == "1" {

	}

	c.Ctx.JSON(iris.Map{"flag": true, "msg": "OK"})
	return
}

// GetCalendar  投资日历功能
func (c *CalendarController) GetCalendar() mvc.Result {

	today := time.Now().Format("2006-01-02")
	return mvc.View{
		Name: "stocks/stocks.calendar.html",
		Data: iris.Map{
			"Title": "投资日历",
			"Today": today,
		},
	}
}

// SaveCalendarEventForm 保存日历事件，以显示在日历控件中
func (c *CalendarController) SaveCalendarEventForm() {

	date := c.Ctx.FormValue("date")
	bgcolor := c.Ctx.FormValue("bgcolor")
	content := c.Ctx.FormValue("content")
	eventid := c.Ctx.FormValue("event_id")

	isAddEvent := len(eventid) == 0
	isEditEvent := len(eventid) > 0
	isDeleteEvent := isEditEvent && content == "d"

	_, user := utils.UserCache(c.Ctx)

	envmodel := models.STK_CALENDAR_EVENTS{
		UserId:     user.UserID,
		EvtDate:    date,
		EvtColor:   bgcolor,
		EvtContent: content,
	}

	db := app.GetDB()
	// defer db.Close()

	action := "add"

	if isDeleteEvent {
		envmodel.EvtID = conv.ToInt64(eventid, -1)
		db.Delete(&envmodel)
		action = "del"
	} else if isAddEvent {
		db.Create(&envmodel)
		action = "add"
	} else if isEditEvent {
		envmodel.EvtID = conv.ToInt64(eventid, -1)
		db.Save(&envmodel)
		action = "edt"
	}

	c.Ctx.JSON(iris.Map{"flag": true, "msg": "OK", "id": envmodel.EvtID, "action": action})
	return
}

// GetCalendarEventList 得到日历日志数据集合，以显示在日历中
func (c *CalendarController) GetCalendarEventList() {

	start := c.Ctx.FormValue("start")
	end := c.Ctx.FormValue("end")

	if len(start) == 0 {
		start = "2020-10-01"
	}
	if len(end) == 0 {
		end = "2020-10-01"
	}

	db := app.GetDB()
	// defer db.Close()

	_, user := utils.UserCache(c.Ctx)

	var datas []models.STK_CALENDAR_EVENTS
	db.Where("user_id = ? and evt_date BETWEEN ? AND ?", user.UserID, start, end).Order("evt_date asc").Find(&datas)
	c.Ctx.JSON(iris.Map{"flag": true, "msg": "OK", "data": datas})

	return
}
